package com.quan.admin.web.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.quan.admin.commons.vo.SysRolePermissionVo;
import com.quan.admin.entity.SysRole;
import com.quan.admin.entity.SysRolePermission;
import com.quan.admin.service.SysRolePermissionService;
import com.quan.admin.service.SysRoleService;
import com.quan.commons.core.bean.R;
import com.quan.commons.core.biz.support.MyBaseController;
import com.quan.commons.core.utils.PageUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



/**
 * 系统-角色
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2020-08-08 20:15:07
 */
@Api(tags = "系统-角色")
@RestController
@RequestMapping("sysrole")
public class SysRoleController extends MyBaseController {

    @Autowired
    private SysRoleService service;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    /**
     * 自定义分页列表
     * @param entity
     * @return
     */
    @GetMapping(value = "selectMyPage")
    public Object selectMyPage(SysRole entity) {
        IPage<SysRole> page = this.service.selectMyPage(new PageUtils(request), entity);
        return R.ok().put("data", page);
    }

    /**
     * 分页列表
     * @param entity
     * @return
     */
    @GetMapping(value = "page")
    public Object page(SysRole entity) {
        IPage<SysRole> page = this.service.selectPage(new PageUtils(request), entity);
        return R.ok().put("data", page);
    }

    /**
     * 列表
     * @param entity
     * @return
     */
    @GetMapping(value = "list")
    public Object list(SysRole entity) {
        List<SysRole> list = this.service.list(new PageUtils(request), entity);
        return R.ok().put("data", list);
    }


    /**
     * 信息
     * @param {id}
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SysRole systemRole = this.service.getById(id);
        return R.ok().put("data", systemRole);
    }

    /**
     * 保存
     * @param entity
     * @param bindingResult
     * @return
     */
    @PostMapping("save")
    public R save(@Valid @RequestBody SysRole entity, BindingResult bindingResult){
		this.service.save(entity);
        return R.ok();
    }

    /**
     * 修改
     * @param entity
     * @param bindingResult
     * @return
     */
    @PostMapping("update")
    public R update(@Valid @RequestBody SysRole entity, BindingResult bindingResult){
		this.service.updateById(entity);
        return R.ok();
    }

    /**
     * 删除
     * @param params idList: [1,2,3]
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestBody Map<String, Object> params) {
        if (null != params && !params.isEmpty()) {
            List<Long> idList = JSON.parseArray(params.get("idList").toString(), Long.class);
            this.service.removeByIds(idList);
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 根据角色ID获取对应的权限
     * @param roleId
     * @return
     */
    @GetMapping(value = "/getPermissionByRoleId/{roleId}")
    public Object getPermissionByRoleId(@PathVariable Long roleId) {
        return R.ok().put("data", this.sysRolePermissionService.getPermissionByRoleId(roleId));
    }


    /**
     * 保存角对应的菜单权限
     * @param vo
     * @return
     */
    @PostMapping("/saveRolePermission")
    public Object saveRolePermission(@RequestBody SysRolePermissionVo vo) {

        //先把该角色对于的权限全部删除
        this.sysRolePermissionService.deleteByRoleId(vo.getRoleId());

        if(null != vo.getPermissions()) {
            List<SysRolePermission> list = new ArrayList<SysRolePermission>();

            for (String permission : vo.getPermissions()) {
                String[] split = permission.split("-") ;
                SysRolePermission entity = new SysRolePermission() ;
                entity.setRoleId(vo.getRoleId());
                entity.setMenuId(Long.parseLong(split[0]));
                entity.setPermissionId(Long.parseLong(split[1]));
                list.add(entity);
            }
            this.sysRolePermissionService.saveRolePermission(list);
            return R.ok();
        }
        return R.failure("保存角色对应的菜单权限失败");
    }

}
